/**
 *    \file        AKSoundController.m
 *
 *    \section  desc_sec Description
 *    Description 
 *
 *
 *  \if copyright
 *    Copyright:    (c) 2008 FileMaker, Inc. All rights reserved.
 *
 *    FILEMAKER CONFIDENTIAL
 *    This file includes unpublished proprietary source code of FileMaker, Inc.
 *    The copyright notice above does not evidence any actual or intended publication
 *    of such source code.
 *  \endif
 *    
 *  \section hist_sec Change History:
 *
 *    -     7/28/08        BC        Created this file
 *
 *  <hr>
 * \addtogroup AnimalKingdom
 */

#import "AKSoundController.h"

@implementation AKSoundController

- (id)init {
	self = [super init];
	if (self != nil) {
		foregroundSounds = [[NSMutableSet alloc] init];
		backgroundSounds = [[NSMutableSet alloc] init];
		allowMultipleForegroundSounds = NO;
	}
	return self;
}

- (void)dealloc {
	[foregroundSounds release]; 
	[backgroundSounds release];
	[super dealloc];
}

- (BOOL)allowMultipleForegroundSounds {
	return allowMultipleForegroundSounds;
}

- (void)setAllowMultipleForegroundSounds:(BOOL)flag {
	allowMultipleForegroundSounds = flag;
}

- (unsigned int)numberOfSounds {
	return [foregroundSounds count] + [backgroundSounds count];
}

- (unsigned int)numberOfForegroundSounds {
	return [foregroundSounds count];
}

- (unsigned int)numberOfBackgroundSounds {
	return [backgroundSounds count];
}

- (void)playSound:(NSSound*)aSound continuously:(BOOL)continuously inBackground:(BOOL)inBackground {
//	NSAssert1((![aSound isPlaying]), @"sound:%@ is still playing upon being added to sound controller!", aSound);
//	NSAssert1((![foregroundSounds containsObject:aSound] && ![backgroundSounds containsObject:aSound]), @"sound:%@ is still playing under the control of the sound controller", aSound);
	
	[aSound setDelegate:self];
	
	if (inBackground) {
		[backgroundSounds addObject:aSound];
	} else {
		if (allowMultipleForegroundSounds)
			[self stopAllForegroundSounds];
		[foregroundSounds addObject:aSound];
	}
	
	[aSound setLoops:continuously];
	[aSound play];
}

- (void)stopSound:(NSSound*)sound {
	if ([foregroundSounds containsObject:sound])
		[foregroundSounds removeObject:sound];
	else if ([backgroundSounds containsObject:sound])
		[backgroundSounds removeObject:sound];
}

- (void)stopAllSounds {
	[self stopAllForegroundSounds];
	[self stopAllBackgroundSounds];
}

- (void)stopAllForegroundSounds {
	for (NSSound *aSound in foregroundSounds) {
		[aSound stop];
		[aSound setDelegate:nil];
	}
	
	[foregroundSounds removeAllObjects];
}

- (void)stopAllBackgroundSounds {
	for (NSSound *aSound in backgroundSounds) {
		[aSound stop];
		[aSound setDelegate:nil];
	}
	
	[backgroundSounds removeAllObjects];
}

#pragma mark -
#pragma mark Delegate method

- (void)sound:(NSSound *)sound didFinishPlaying:(BOOL)finishedPlaying {
	if ([foregroundSounds containsObject:sound]) {
		[sound setDelegate:nil];
		[foregroundSounds removeObject:sound];
	} else if ([backgroundSounds containsObject:sound]) {
		[sound setDelegate:nil];
		[backgroundSounds removeObject:sound];
	}
}

@end
